package com.xhtt.modules.task.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhtt.common.exception.RRException;
import com.xhtt.common.file.FileInfoModel;
import com.xhtt.common.utils.CopyUtils;
import com.xhtt.common.utils.DateUtils;
import com.xhtt.common.utils.PageUtils;
import com.xhtt.common.utils.Query;
import com.xhtt.modules.cfg.entity.*;
import com.xhtt.modules.cfg.service.*;
import com.xhtt.modules.sys.entity.SysUserEntity;
import com.xhtt.modules.task.dao.TaskShipPlanMapper;
import com.xhtt.modules.task.entity.*;
import com.xhtt.modules.task.model.lmtask.BusinessCheckModel;
import com.xhtt.modules.task.model.lmtask.TaskOperLMModel;
import com.xhtt.modules.task.model.ship.ArrangeModel;
import com.xhtt.modules.task.service.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 船计划 服务实现类
 * </p>
 *
 * @author feipc
 * @since 2019-02-27
 */
@Service
public class TaskShipPlanServiceImpl extends ServiceImpl<TaskShipPlanMapper, TaskShipPlan> implements TaskShipPlanService {
    @Autowired
    private TaskShipPlanProductService taskShipPlanProductService;
    @Autowired
    private TaskShipTaskService taskShipTaskService;
    @Autowired
    private TaskShipTaskProdStepService taskShipProdStepService;
    @Autowired
    private CfgTaskSubService taskSubService;
    @Autowired
    private CfgTaskInfoService taskInfoService;
    @Autowired
    private CfgBerthInfoService cfgBerthInfoService;
    @Autowired
    private CfgComprodService cfgComprodService;
    @Resource
    private CfgPotInfoService cfgPotInfoService;
    @Autowired
    private TransactionTemplate transactionTemplate;
    @Autowired
    private TaskExecutor taskExecutor;
    @Autowired
    private CfgWarnTaskService warnTaskService;
    @Autowired
    private TaskShipSeoLogService taskShipSeoLogService;
    @Autowired
    private TaskShipSeoService taskShipSeoService;
    @Autowired
    private TaskShipTaskProdStepService shipTaskStepService;


    @Override
    public PageUtils planList(Map<String, Object> params, SysUserEntity user) {
        Object status = params.get("status");
        if (null != status && status.toString().equals("2"))//分配任务页面，执行班组的人可以看到安排给自己班组的任务，其他人看不到
        {
            params.put("teamId", user.getTeamId());
        }
        Page<TaskShipPlan> page = new Query<TaskShipPlan>(params).getPage();
        page.setRecords(baseMapper.planList(page, params));
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void addPlan(TaskShipPlan taskShipPlan) {
        List<TaskShipPlanProduct> productList = taskShipPlan.getProductList();

        //设置主任务（计划）名称
        if (1 == taskShipPlan.getType().intValue()) {
            taskShipPlan.setTaskName(taskShipPlan.getShipName() + "-装船");
        } else {
            taskShipPlan.setTaskName(taskShipPlan.getShipName() + "-卸船");
        }

        this.save(taskShipPlan);
        Integer planId = taskShipPlan.getId();
        if (CollectionUtils.isNotEmpty(productList)) {
            productList.forEach(taskShipPlanProduct -> taskShipPlanProduct.setShipPlanId(planId));
            taskShipPlanProductService.saveBatch(productList);
        }
    }

    @Override
    @Transactional
    public void deletePlan(List<Integer> planIds) {
        //对产品全删
        taskShipPlanProductService.remove(new LambdaQueryWrapper<TaskShipPlanProduct>().in(TaskShipPlanProduct::getShipPlanId, planIds));
        //子任务
        taskShipTaskService.remove(new LambdaQueryWrapper<TaskShipTask>().in(TaskShipTask::getShipPlanId, planIds));

        this.removeByIds(planIds);
    }

    @Override
    public TaskShipPlan getPlan(Integer planId) {
        TaskShipPlan taskShipPlan = this.getById(planId);
        if (null == taskShipPlan) {
            throw new RRException("对象不存在");
        }
        String photo = taskShipPlan.getPhoto();
        if (StringUtils.isNotEmpty(photo)) {
            taskShipPlan.setPhotoFiles(JSON.parseArray(photo, FileInfoModel.class));
        }

        List<TaskShipPlanProduct> productList = taskShipPlanProductService.list(new LambdaQueryWrapper<TaskShipPlanProduct>().eq(TaskShipPlanProduct::getShipPlanId, planId));
        if (CollectionUtils.isNotEmpty(productList)) {
            taskShipPlan.setProductList(productList);
        }
        return taskShipPlan;
    }

    @Override
    @Transactional
    public void updatePlan(TaskShipPlan taskShipPlan) {
        Integer planId = taskShipPlan.getId();
        if (null == planId) {
            throw new RRException("参数有误");
        }
        TaskShipPlan db = this.getById(planId);
        if (null == db) {
            throw new RRException("对象不存在");
        }
        //设置主任务（计划）名称
        if (1 == taskShipPlan.getType().intValue()) {
            taskShipPlan.setTaskName(taskShipPlan.getShipName() + "-装船");
        } else {
            taskShipPlan.setTaskName(taskShipPlan.getShipName() + "-卸船");
        }

        List<FileInfoModel> photoFileList = taskShipPlan.getPhotoFiles();
        if (CollectionUtils.isNotEmpty(photoFileList)) {
            taskShipPlan.setPhoto(JSON.toJSONString(photoFileList));
        } else {
            taskShipPlan.setPhoto(JSON.toJSONString(Collections.EMPTY_LIST));
        }
        CopyUtils.copyProperties(taskShipPlan, db);
        this.updateById(db);
        //全删全增 产品
        List<TaskShipPlanProduct> productList = taskShipPlan.getProductList();
        taskShipPlanProductService.remove(new LambdaQueryWrapper<TaskShipPlanProduct>().eq(TaskShipPlanProduct::getShipPlanId, planId));
        if (CollectionUtils.isNotEmpty(productList)) {
            productList.forEach(taskShipPlanProduct -> taskShipPlanProduct.setShipPlanId(planId));
            taskShipPlanProductService.saveBatch(productList);
        }

    }

    @Override
    public ArrangeModel getArrangeInfo(Integer id) {
        ArrangeModel model = new ArrangeModel();
        TaskShipPlan taskShipPlan = this.getById(id);
        if (null == taskShipPlan) {
            throw new RRException("对象不存在");
        }
//        cfgBerthInfoService.getById(taskShipPlan.getBerthId());//泊位信息
        List<TaskShipPlanProduct> shipPlanProducts = taskShipPlanProductService.list(new LambdaQueryWrapper<TaskShipPlanProduct>().eq(TaskShipPlanProduct::getShipPlanId, id));//产品+罐信息

        model.setPlanId(id);
        model.setMainTaskName(taskShipPlan.getTaskName());

        String typeName = 1 == taskShipPlan.getType().intValue() ? "装船" : "卸船";
        CfgTaskInfo cfgTaskInfo = taskInfoService.getOne(new LambdaQueryWrapper<CfgTaskInfo>().eq(CfgTaskInfo::getName, typeName).select(CfgTaskInfo::getId));
        if (null == cfgTaskInfo) {
            throw new RRException("计划任务配置有误");
        }
        List<TaskShipTask> tasks = new ArrayList<>();
        //找到配置好的要做的任务
        List<CfgTaskSub> taskSubList = taskSubService.list(new LambdaQueryWrapper<CfgTaskSub>().eq(CfgTaskSub::getTaskId, cfgTaskInfo.getId()));

        TaskShipTask shipTask;
        for (CfgTaskSub cfgTaskSub : taskSubList) {
            if (cfgTaskSub.getStatus().intValue() != 1) {
                continue;//删除状态的就不管
            }
            //排管任务进一步处理，其他任务就不用处理了
            if (cfgTaskSub.getType().intValue() == 1) {
                // 遍历产品,设置连接的头尾
                for (TaskShipPlanProduct planProduct : shipPlanProducts) {
                    List<TaskShipTaskProdStep> stepList = new ArrayList<>();
                    shipTask = new TaskShipTask();
                    shipTask.setName(cfgTaskSub.getName());
                    shipTask.setType(cfgTaskSub.getType());
                    //构造罐
                    TaskShipTaskProdStep stepPot = new TaskShipTaskProdStep();
                    stepPot.setType(1);//1罐2硬3泵4码5软
                    stepPot.setProductId(planProduct.getProductId());
                    stepPot.setProductName(planProduct.getProductName());
                    stepPot.setTypeId(planProduct.getPotId());
                    stepPot.setTypeName(planProduct.getPotName());
                    //构造泊位
                    TaskShipTaskProdStep stepBerth = new TaskShipTaskProdStep();
                    stepBerth.setType(4);//1罐2硬3泵4码5软
                    stepBerth.setProductId(planProduct.getProductId());
                    stepBerth.setProductName(planProduct.getProductName());
                    stepBerth.setTypeId(taskShipPlan.getBerthId());
                    stepBerth.setTypeName(taskShipPlan.getBerthName());
//                    //装船==罐--泊位  这个人性化的设计居然不要？到时候要改回来的话说明产品经理就是猪
//                    if (1 == taskShipPlan.getType().intValue()) {
//                        //设置第一个连接点-罐
//                        stepList.add(stepPot);
//                        //设置最后一个连接点-泊位
//                        stepList.add(stepBerth);
//                    }
//                    //卸船==泊位--船
//                    else {
//                        //设置第一个连接点-泊位
//                        stepList.add(stepBerth);
//                        //设置最后一个连接点-罐
//                        stepList.add(stepPot);
//                    }
                    //设置第一个连接点-罐
                    stepList.add(stepPot);
                    //设置最后一个连接点-泊位
                    stepList.add(stepBerth);

                    shipTask.setProductId(planProduct.getProductId());
                    shipTask.setProductName(planProduct.getProductName());
                    shipTask.setTaskProdSteps(stepList);
                    shipTask.setPlanProductId(planProduct.getId());//关联
                    tasks.add(shipTask);
                }

            } else if (cfgTaskSub.getType().intValue() == 5)//新加的 狗日的打尺取样任务
            {
                // 遍历产品
                for (TaskShipPlanProduct planProduct : shipPlanProducts) {
                    shipTask = new TaskShipTask();
                    shipTask.setName(cfgTaskSub.getName());
                    shipTask.setType(cfgTaskSub.getType());
                    shipTask.setProductId(planProduct.getProductId());
                    shipTask.setProductName(planProduct.getProductName());
                    shipTask.setPotName(planProduct.getPotName());
                    shipTask.setPlanProductId(planProduct.getId());//关联
                    tasks.add(shipTask);
                }
            } else {
                shipTask = new TaskShipTask();
                shipTask.setName(cfgTaskSub.getName());
                shipTask.setType(cfgTaskSub.getType());
                tasks.add(shipTask);
            }
        }
        model.setTasks(tasks);

        return model;
    }

    @Override
    public void arrange(ArrangeModel arrangeModel) {
        Integer planId = arrangeModel.getPlanId();
        List<TaskShipTask> ts = transactionTemplate.execute(transactionStatus -> {
            try {
                TaskShipPlan shipPlan1 = getById(planId);
                if (null == shipPlan1) {
                    throw new RRException("对象不存在");
                }
                shipPlan1.setTaskName(arrangeModel.getMainTaskName());
                shipPlan1.setStatus(2);//1待安排2待分配3退回4进行中9完成//
                updateById(shipPlan1);//跟新个名字而已

                //
                List<TaskShipTask> tasks = arrangeModel.getTasks();
                tasks.forEach(taskShipTask -> taskShipTask.setShipPlanId(planId));
                //创建之前看一下有没有创建过了小任务 ，有的话全删除，并记录下taskIds，因为后面还要删步骤
                List<TaskShipTask> oldTasks = taskShipTaskService.list(new LambdaQueryWrapper<TaskShipTask>().eq(TaskShipTask::getShipPlanId, planId).select(TaskShipTask::getId));
                if (CollectionUtils.isNotEmpty(oldTasks)) {
                    //取taskIds
                    List<Integer> taskIds = oldTasks.stream().map(taskShipTask -> taskShipTask.getId()).collect(Collectors.toList());
                    //删除tasks
                    taskShipTaskService.removeByIds(taskIds);
                    // 获取steps
//                    List<TaskShipTaskProdStep> steps = taskShipProdStepService.list(new LambdaQueryWrapper<TaskShipTaskProdStep>().in(TaskShipTaskProdStep::getShipTaskId, taskIds));
                    //还要把steps里面关联的节点还原成未使用
//                    if (CollectionUtils.isNotEmpty(steps)) {
//                        conPointService.releaseShipPoint(steps);//去掉了 因为业务变了 回退不会影响管子的使用了
//                    }
                    //删除steps
                    taskShipProdStepService.remove(new LambdaQueryWrapper<TaskShipTaskProdStep>().in(TaskShipTaskProdStep::getShipTaskId, taskIds));
                }

                taskShipTaskService.saveBatch(tasks);//创建小任务

                //但是要保存第一个和最后一个节点，为下一步用
                for (TaskShipTask hoseTask : tasks) {
                    if (1 == hoseTask.getType().intValue()) {
                        List<TaskShipTaskProdStep> planProductList = hoseTask.getTaskProdSteps();//获取输送产品+管线步骤  注意web要在每一步都设置productid和name
                        planProductList.forEach(pp -> pp.setShipTaskId(hoseTask.getId()));
                        shipTaskStepService.saveBatch(planProductList);//保存链接任务步骤
                    }
                }

                return tasks;

            } catch (Throwable e) {
                transactionStatus.setRollbackOnly();
                return null;
            }
        });
        if (null == ts) {
            throw new RRException("处理异常");
        }


        //然后保存软管连接节点们
        //狗日的 又说不要在安排的时候保存软管连接了 草草草草草,
//        for (TaskShipTask hoseTask : ts) {
//            if (1 == hoseTask.getType().intValue()) {
//                List<TaskShipTaskProdStep> planProductList = hoseTask.getTaskProdSteps();//获取输送产品+管线步骤  注意web要在每一步都设置productid和name
//                taskStepService.updateSteps(planProductList, hoseTask.getId());
//            }
//        }
    }

    @Override
    public void back(TaskShipPlan plan) {
        TaskShipPlan np = new TaskShipPlan();
        np.setId(plan.getId());
        np.setStatus(3);//1待安排2待分配3退回4进行中9完成
        np.setBackReason(plan.getBackReason());
        this.updateById(np);

        //狗日的 分配的时候才保存软管连接 ，所以回退不涉及了
        //解放管子和泵
//        taskExecutor.execute(() -> {
//            List<TaskShipTask> taskList = taskShipTaskService.list(new LambdaQueryWrapper<TaskShipTask>().eq(TaskShipTask::getShipPlanId, plan.getId()).eq(TaskShipTask::getType, 1).select(TaskShipTask::getId));//拿到排管任务id
//            if (CollectionUtils.isNotEmpty(taskList)) {
//                List<Integer> taskIds = taskList.stream().map(taskShipTask -> taskShipTask.getId()).collect(Collectors.toList());
//                //然后拿节点
//                List<TaskShipTaskProdStep> steps = taskShipProdStepService.list(new LambdaQueryWrapper<TaskShipTaskProdStep>().in(TaskShipTaskProdStep::getShipTaskId, taskIds).in(TaskShipTaskProdStep::getType, Arrays.asList(new Integer[]{2, 3, 5})));
//                for (TaskShipTaskProdStep step : steps) {
//                    if (step.getType().intValue() == 2) {
//                        CfgTubeInfo tube = new CfgTubeInfo();
//                        tube.setUsed(0);
//                        cfgTubeInfoService.update(tube, new LambdaQueryWrapper<CfgTubeInfo>().eq(CfgTubeInfo::getId, step.getTypeId()));
//                    } else if (step.getType().intValue() == 5) {
//                        CfgHoseInfo hose = new CfgHoseInfo();
//                        hose.setUsed(0);
//                        cfgHoseInfoService.update(hose, new LambdaQueryWrapper<CfgHoseInfo>().eq(CfgHoseInfo::getId, step.getTypeId()));
//                    } else if (step.getType().intValue() == 3) {
//                        CfgPumpInfo pump = new CfgPumpInfo();
//                        pump.setUsed(0);
//                        cfgPumpInfoService.update(pump, new LambdaQueryWrapper<CfgPumpInfo>().eq(CfgPumpInfo::getId, step.getTypeId()));
//                    }
//                }
//            }
//
//        });
    }

    @Override
    @Transactional
    public void addPlanFromOut(TaskShipPlan taskShipPlan) {
        //设置主任务（计划）名称
        if (1 == taskShipPlan.getType().intValue()) {
            taskShipPlan.setTaskName(taskShipPlan.getShipName() + "-装船");
        } else {
            taskShipPlan.setTaskName(taskShipPlan.getShipName() + "-卸船");
        }
        String berthName = taskShipPlan.getBerthName();
        if (StringUtils.isEmpty(berthName)) {
            throw new RRException("泊位不能为空");
        }
        CfgBerthInfo cfgBerthInfo = cfgBerthInfoService.getOne(new LambdaQueryWrapper<CfgBerthInfo>().eq(CfgBerthInfo::getNumberNo, berthName).eq(CfgBerthInfo::getStatus, 1));
        if (null == cfgBerthInfo) {
            throw new RRException("无效的泊位");
        }
        taskShipPlan.setBerthId(cfgBerthInfo.getId());
        this.save(taskShipPlan);

        List<TaskShipPlanProduct> productList = taskShipPlan.getProductList();
        Integer planId = taskShipPlan.getId();
        if (CollectionUtils.isNotEmpty(productList)) {
            for (TaskShipPlanProduct product : productList) {
                String productName = product.getProductName();
                if (StringUtils.isEmpty(productName)) {
                    throw new RRException("产品不能为空");
                }
                String potName = product.getPotName();
                if (StringUtils.isEmpty(potName)) {
                    throw new RRException("罐不能为空");
                }
                CfgComprod cfgComprod = cfgComprodService.getByName(productName);
                if (null == cfgComprod) {
                    throw new RRException("无效的产品");
                }
                product.setProductId(cfgComprod.getId());

                CfgPotInfo cfgPotInfo = cfgPotInfoService.getOne(new LambdaQueryWrapper<CfgPotInfo>().eq(CfgPotInfo::getNumberNo, potName).eq(CfgPotInfo::getStatus, 1));
                if (null == cfgPotInfo) {
                    throw new RRException("无效的罐");
                }
                product.setPotId(cfgPotInfo.getId());

                product.setShipPlanId(planId);
            }
            taskShipPlanProductService.saveBatch(productList);
        }
    }

    @Override
    public PageUtils seoList(Map<String, Object> params) {
        Page<TaskShipSeo> page = new Query<TaskShipSeo>(params).getPage();
        List<TaskShipSeo> list = baseMapper.seoList(page, params);
        Set<Integer> spIds = new HashSet<>();
        for (TaskShipSeo seo : list) {
            if ("seo".equals(seo.getSource())) {
                continue;
            }
            Integer spId = seo.getSpId();
            if (!spIds.contains(spId))//不包含，那就设置lm任务的详情
            {
                spIds.add(spId);//先放进去
                String operDetail = seo.getOperDetail();
                if (StringUtils.isEmpty(operDetail)) {
                    continue;
                }
                TaskOperLMModel lmModel = JSON.parseObject(operDetail, TaskOperLMModel.class);
                List<BusinessCheckModel> bcList = lmModel.getBusinessCheckModelList();
                seo.setKb(DateUtils.NA_DATE.equals(lmModel.getKb()) ? null : lmModel.getKb());
                seo.setLb(DateUtils.NA_DATE.equals(lmModel.getLb()) ? null : lmModel.getLb());
                seo.setDlsc(DateUtils.NA_DATE.equals(lmModel.getDlsc()) ? null : lmModel.getDlsc());
                seo.setLmdl(DateUtils.NA_DATE.equals(lmModel.getLmdl()) ? null : lmModel.getLmdl());
                seo.setAqhy(DateUtils.NA_DATE.equals(lmModel.getAqhy()) ? null : lmModel.getAqhy());
                if (CollectionUtils.isNotEmpty(bcList)) {
                    //遍历出各个最大最小值 ，只要不是全为null 就一定有最小值，只要一个为null 那就没有最大值
                    //计量完成
                    LocalDateTime jlwc = null;
                    for (BusinessCheckModel bc : bcList) {
                        LocalDateTime temp = bc.getJlwc();
                        if (null == temp) {
                            jlwc = null;
                            break;
                        } else {
                            if (null == jlwc || temp.compareTo(jlwc) > 0) {//如果是第一次进入判断或者这次的值大于之前的
                                jlwc = temp;
                            }
                        }
                    }
                    seo.setJlwc(DateUtils.NA_DATE.equals(jlwc) ? null : jlwc);
                    //停泵
                    LocalDateTime tb = null;
                    for (BusinessCheckModel bc : bcList) {
                        LocalDateTime temp = bc.getTb();
                        if (null == temp) {
                            tb = null;
                            break;
                        } else {
                            if (null == tb || temp.compareTo(jlwc) > 0) {
                                tb = temp;
                            }
                        }
                    }
                    seo.setTb(DateUtils.NA_DATE.equals(tb) ? null : tb);
                    //商检达到验舱时间
                    LocalDateTime sjddyc = null;
                    for (BusinessCheckModel bc : bcList) {
                        LocalDateTime temp = bc.getSjddyc();
                        if (null == temp) {
                            sjddyc = null;
                            break;
                        } else {
                            if (null == sjddyc || temp.compareTo(sjddyc) > 0) {
                                sjddyc = temp;
                            }
                        }
                    }
                    seo.setSjddyc(DateUtils.NA_DATE.equals(sjddyc) ? null : sjddyc);
                    //拆管时间
                    LocalDateTime cg = null;
                    for (BusinessCheckModel bc : bcList) {
                        LocalDateTime temp = bc.getCg();
                        if (null == temp) {
                            cg = null;
                            break;
                        } else {
                            if (null == cg || temp.compareTo(cg) > 0) {
                                cg = temp;
                            }
                        }
                    }
                    seo.setCg(DateUtils.NA_DATE.equals(cg) ? null : cg);

                    //起泵时间（最早）
                    LocalDateTime qb = null;
                    for (BusinessCheckModel bc : bcList) {
                        LocalDateTime temp = bc.getQb();
                        if (null == temp) {
                            continue;
                        } else {
                            if (null == qb || temp.compareTo(qb) < 0) {
                                qb = temp;
                            }
                        }
                    }
                    seo.setQb(DateUtils.NA_DATE.equals(qb) ? null : qb);
                }
            } else {//否则就清理一下多余的数据
                seo.setLmUser(null);
                seo.setLmStatus(null);
                seo.setBallStatus(null);
                seo.setBallStartTime(null);
                seo.setBallFinishTime(null);
                seo.setCcrStatus(null);
                seo.setPlanKb(null);
                seo.setKb(null);
                seo.setDlsc(null);
                seo.setJlwc(null);
                seo.setLmdl(null);
                seo.setAqhy(null);
                seo.setQb(null);
                seo.setTb(null);
                seo.setSjddyc(null);
                seo.setCg(null);
                seo.setLb(null);
            }
        }
        //遍历 设置报警
        List<CfgWarnTask> warnTaskList = warnTaskService.list();
        Map<String, Integer> warnStartMap = new HashMap<>();
        Map<String, Integer> warnFinishMap = new HashMap<>();
        for (CfgWarnTask wt : warnTaskList) {
            warnStartMap.put(wt.getPlan() + "-" + wt.getPlanType() + "-" + wt.getTaskType(), wt.getTime1());
            warnFinishMap.put(wt.getPlan() + "-" + wt.getPlanType() + "-" + wt.getTaskType(), wt.getTime2());
        }
        for (TaskShipSeo seo : list) {
            Integer timeHoseStart = warnStartMap.get("ship-" + seo.getPlanType() + "-1");//这里数据库配好了 管线就是1，所以写死了
            if (null != timeHoseStart && timeHoseStart.intValue() > 0) {
                LocalDateTime hosePLanStartDate = seo.getHosePlanStartTime();
                LocalDateTime startTime = seo.getHoseStartTime();
                if (null != hosePLanStartDate && null != startTime) {
                    LocalDateTime deadline = hosePLanStartDate.plusMinutes(timeHoseStart);
                    if (startTime.compareTo(deadline) > 0) {
                        seo.setHoseStartTimeIfWarn(1);
                    }
                }
            }
            Integer timeBallStart = warnStartMap.get("ship-" + seo.getPlanType() + "-3");//这里数据库配好了 通球就是3，所以写死了
            if (null != timeBallStart && timeBallStart.intValue() > 0) {
                LocalDateTime ballPLanStartDate = seo.getBallPlanStartTime();
                LocalDateTime startTime = seo.getBallStartTime();
                if (null != ballPLanStartDate && null != startTime) {
                    LocalDateTime deadline = ballPLanStartDate.plusMinutes(timeBallStart);
                    if (startTime.compareTo(deadline) > 0) {
                        seo.setBallStartTimeIfWarn(1);
                    }
                }
            }
            Integer timeDcqyStart = warnStartMap.get("ship-" + seo.getPlanType() + "-5");//这里数据库配好了 打尺取样就是5，所以写死了
            if (null != timeDcqyStart && timeDcqyStart.intValue() > 0) {
                LocalDateTime dcqyPlanStartTime = seo.getDcqyPlanStartTime();
                LocalDateTime startTime = seo.getDcqyStartTime();
                if (null != dcqyPlanStartTime && null != startTime) {
                    LocalDateTime deadline = dcqyPlanStartTime.plusMinutes(timeDcqyStart);
                    if (startTime.compareTo(deadline) > 0) {
                        seo.setDcqyStartTimeIfWarn(1);
                    }
                }
            }

            Integer timeHoseFinish = warnFinishMap.get("ship-" + seo.getPlanType() + "-1");//这里数据库配好了 管线就是1，所以写死了
            if (null != timeHoseFinish && timeHoseFinish.intValue() > 0) {
                LocalDateTime finishTime = seo.getHoseFinishTime();
                LocalDateTime startTime = seo.getHoseStartTime();
                if (null != finishTime && null != startTime) {
                    LocalDateTime deadline = startTime.plusMinutes(timeHoseFinish);
                    if (finishTime.compareTo(deadline) > 0) {
                        seo.setHoseFinishTimeIfWarn(1);
                    }
                }
            }

            Integer timeBallFinish = warnFinishMap.get("ship-" + seo.getPlanType() + "-3");//这里数据库配好了 通球就是3，所以写死了
            if (null != timeBallFinish && timeBallFinish.intValue() > 0) {
                LocalDateTime finishTime = seo.getBallFinishTime();
                LocalDateTime startTime = seo.getBallStartTime();
                if (null != finishTime && null != startTime) {
                    LocalDateTime deadline = startTime.plusMinutes(timeBallFinish);
                    if (finishTime.compareTo(deadline) > 0) {
                        seo.setBallFinishTimeIfWarn(1);
                    }
                }
            }

            Integer timeDcqyFinish = warnFinishMap.get("ship-" + seo.getPlanType() + "-5");//这里数据库配好了 打尺取样就是5，所以写死了
            if (null != timeDcqyFinish && timeDcqyFinish.intValue() > 0) {
                LocalDateTime finishTime = seo.getDcqyFinishTime();
                LocalDateTime startTime = seo.getDcqyStartTime();
                if (null != finishTime && null != startTime) {
                    LocalDateTime deadline = startTime.plusMinutes(timeDcqyFinish);
                    if (finishTime.compareTo(deadline) > 0) {
                        seo.setDcqyFinishTimeIfWarn(1);
                    }
                }
            }
        }

        page.setRecords(list);
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void seoEdit(TaskShipSeo seo, SysUserEntity user) {
        taskShipSeoService.saveOrUpdate(seo);
        TaskShipSeoLog log = new TaskShipSeoLog();
        log.setSppId(seo.getSppId());
        log.setUserId(user.getUserId().intValue());
        log.setUserName(user.getName());
        taskShipSeoLogService.save(log);
    }
}
